package com.million.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.million.eduservice.entity.EduSubject;
import com.million.eduservice.entity.excel.SubjectData;
import com.million.eduservice.entity.vo.subject.SubjectNestedVo;
import com.million.eduservice.entity.vo.subject.SubjectVo;
import com.million.eduservice.listener.SubjectExcelListener;
import com.million.eduservice.mapper.EduSubjectMapper;
import com.million.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-03-23
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程 =>上传文件
    @Override
    public void saveSubject (MultipartFile file) {
        //创建输入流
        try {
            InputStream inputStream = file.getInputStream();
            //读取文件
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(this)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 返回一级分类和二级分类  嵌套数据
     *
     * @return list集合
     */
    @Override
    public List<SubjectNestedVo> getAllSubject () {
        //返回的数据表
        ArrayList<SubjectNestedVo> subjectNestedVoArrayList = new ArrayList<>();
        //获取一级分类的数据
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        //按照sort 升序排序
        wrapper.orderByAsc("sort", "id");
        List<EduSubject> subjectsOne = baseMapper.selectList(wrapper);

        //获取二级分类的数据
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        //查询parent_id不是0的结果
        wrapper2.ne("parent_id", 0);
        //按照sort 升序排序
        wrapper2.orderByAsc("sort", "id");
        List<EduSubject> subjectsTwo = baseMapper.selectList(wrapper2);

        //填充一级分类数据
        int countOne = subjectsOne.size();
        for (int i = 0; i < countOne; i++) {
            EduSubject eduSubject = subjectsOne.get(i);

            //创建一级类别vo对象
            SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
            BeanUtils.copyProperties(eduSubject,subjectNestedVo);

            //填充二级分类数据
            ArrayList<SubjectVo> subjectVosList = new ArrayList<>();
            int countTwo = subjectsTwo.size();
            for (int j = 0; j < countTwo; j++) {
                EduSubject eduSubject1 = subjectsTwo.get(j);
                if(eduSubject1.getParentId().equals(eduSubject.getId())){
                    SubjectVo subjectVo = new SubjectVo();
                    BeanUtils.copyProperties(eduSubject1,subjectVo);
                    subjectVosList.add(subjectVo);
                }
            }

            subjectNestedVo.setChildren(subjectVosList);
            subjectNestedVoArrayList.add(subjectNestedVo);
        }

        return subjectNestedVoArrayList;
    }
}
